package com.SmsModule.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.ConnectDB;
import util.UtilDAO;

import com.SmsModule.bean.TbCommonPhrase;
 
/**
 * 短信常用短语
 * @author jhc
 *
 */
public class TbSmsTemplateDAO {
	private Connection conn = null;
	private PreparedStatement pt = null; 
	private Statement st = null;
	private ResultSet rs = null;
	private boolean flag = false;
	private String sql = "";
	private TbCommonPhrase smsTemplate=null;
	private List<TbCommonPhrase> smsTemplateList=null;
	
	
	/**
	 * 查询所有常用短语
	 * @param tableName
	 * @param tiaojian
	 * @param order
	 * @return
	 */
	public List<TbCommonPhrase> getTbCommonPhraseAll(String tableName,String tiaojian,String order){
		try {
			conn = ConnectDB.getSqlServerConnection();
			sql = UtilDAO.getList(tableName, order, tiaojian);
			pt = conn.prepareStatement(sql);
			rs = pt.executeQuery();
		
			while(rs.next())
			{  
					if(smsTemplateList==null)
					{
						smsTemplateList = new ArrayList<TbCommonPhrase>();
					}
				smsTemplateList.add(this.getsmsTemplateList(rs));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
           ConnectDB.closeSqlConnection();
        }
		return smsTemplateList;
	}
	
	/**
	 * 传入结果集返回单个信息
	 * @param rs 结果集
	 * @return TbUsers
	 * @throws Exception
	 */
	public TbCommonPhrase getsmsTemplateList(ResultSet rs) throws Exception{
		TbCommonPhrase tbCommonPhrase = new TbCommonPhrase();
		tbCommonPhrase.setPhraseId(rs.getInt("phraseId"));
		tbCommonPhrase.setPhraseContent(rs.getString("phraseContent"));
		tbCommonPhrase.setUseCount(rs.getInt("useCount"));
		tbCommonPhrase.setUserId(rs.getInt("userId"));
		tbCommonPhrase.setCreateTime(rs.getString("createTime"));
		return tbCommonPhrase;				
	}
	
	/**
	 * 插入常用短语
	 * jhc
	 * 2011-1-27 下午01:59:58
	 */
	public int addTbCommonPhrase(TbCommonPhrase tbCommonPhrase){
		int num = 0;
		try {
			conn = ConnectDB.getSqlServerConnection();
			sql = "insert into tb_CommonPhrase(phraseContent,useCount,userId) " +
					"values('"+tbCommonPhrase.getPhraseContent()+"',"+tbCommonPhrase.getUseCount()+","+tbCommonPhrase.getUserId()+")";
			pt = conn.prepareStatement(sql);
			num = pt.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			num = 0;
		}finally {
           ConnectDB.closeSqlConnection();
        }
		return num ;
	}
	
	/**
	 * 单条记录删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean del(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.del(tableName, keyId, idList);
		return bo ;
	}
	/**
	 * 多项删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean delin(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.delin(tableName, keyId, idList);
		return bo ;
	}
	
	/**
	 * 根据选择的常用短语Id修改常用短语内容
	 * jhc
	 * 2011-2-9 上午11:18:21
	 */
	public boolean modifyTemplateContent(String phraseContent,int phraseId){
		boolean bo = false;
		try {
			conn = ConnectDB.getSqlServerConnection();
			sql = "update tb_CommonPhrase set phraseContent='"+phraseContent+"' where phraseId="+phraseId+"";
			pt = conn.prepareStatement(sql);
			pt.execute();
			bo = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			bo = false;
		}finally{
			ConnectDB.closeSqlConnection();
		}
		return bo;
	}
}
